import tool from '@/utils/tool';

/**
 * 是否含有不限分类，有则表示全部允许通过
 */
export function permissionAll() {
	const allPermissions = "*/*/*"
	let permissions = tool.data.get("PERMISSIONS");
	return permissions.includes(allPermissions);
}

/**
 * 比对两组数据是否一致
 *
 * @param obj1
 * @param obj2
 * @returns {boolean}
 */
export function isEqual(obj1, obj2) {
	if (obj1 === obj2) {
		return true;
	}

	if (typeof obj1 !== 'object' || typeof obj2 !== 'object' || obj1 === null || obj2 === null) {
		return false;
	}

	const keys1 = Object.keys(obj1);
	const keys2 = Object.keys(obj2);

	if (keys1.length !== keys2.length) {
		return false;
	}

	for (let key of keys1) {
		if (!keys2.includes(key) || !isEqual(obj1[key], obj2[key])) {
			return false;
		}
	}

	return true;
}

export function permission(data) {
	let permissions = tool.data.get("PERMISSIONS");
	if(!permissions){
		return false;
	}
	return permissions.includes(data);
}

export function rolePermission(data) {
	let userInfo = tool.data.get("USER_INFO");
	if(!userInfo){
		return false;
	}
	let role = userInfo.role;
	if(!role){
		return false;
	}
	return role.includes(data);
}
